<?php

class UserModel extends aBaseModel
{
	private static $_user = null;
	private static $_level = null;
	protected $dbColumns = array(
		'lw_user' => array(
			'name',
			'lid',
			'population',
			'houseNum',
			'food',
			'water',
			'wood',
		),
	);
	private $_table = 'lw_user';
	
	protected function __construct( $userId , $lock = false )
	{
		parent::__construct( $userId , 'user_'. $userId , $lock );
	}
	
	public static function & getInstance( $userId , $lock )
	{
		if ( self::$_user == null )
		{
			self::$_user = new UserModel( $userId , $lock );
		}
		
		if ( !self::$_user->isLocked() )
		{
			self::$_user = new UserModel( $userId , $lock );
		}
		
		if ( $lock )
		{
			ObjectStorage::register( self::$_user );
		} 
		return self::$_user;
	}
	
	/**
	 *获取用户森林里的水资源 
	 * @return int:
	 */
	public function getWater()
	{
		return $this->data['water'];
	}
	
	/**
	 * 获取当前的造人上限
	 * @return int 
	 */
	private function getPolulationLimit()
	{
		return $this->data['houseNum'] * Common::getConfig( 'every_house_can_live' );
	}
	
	/**
	 * 获取用户当前等级信息数组
	 * @return array 
	 */
	public function getLevel()
	{
		if ( !is_array( self::$_level ) )
		{
			self::$_level = Common::getDB( $this->userId )->find( 'level' , array( 'lid' => $this->data['lid'] ) );
		}
		return self::$_level;
	}
	
	/**
	 * 使用木材,如木材数量不足就返回false
	 * @param int $number 使用的数量
	 * @return boolean
	 */
	public function useWood( $number )
	{
		if ( $this->data['wood'] >= $number )
		{
			$this->data['wood'] -= $number;
			$this->updateToDb( $this->_table , self::DATA_ACTION_UPDATE , array( 'wood' ,$this->data['wood'] ) );
			return true;
		}
		return false;
	}
	
	/**
	 * 使用食物,如数量不足就返回false
	 * @param int $number
	 * @return boolean
	 */
	public function useFood( $number )
	{
		if ( $this->data['food'] >= $number )
		{
			$this->data['food'] -= $number;
			$this->updateToDb( $this->_table , self::DATA_ACTION_UPDATE , array( 'food' ,$this->data['food'] ) );
			return true;
		}
		return false;
	}
	
	/**
	 * 改变等级
	 * @param int $newLevel
	 */
	private function changeLevel()
	{
		if ( $this->data['population'] == $this->getLevelPopulationLimit() )
		{
			$this->data['lid']++;
			$this->updateToDb( $this->_table , self::DATA_ACTION_UPDATE , array( 'lid' => $this->data['lid'] ) );
			return true;
		} 
	}
	
	/**
	 * 获取当前人口上限
	 * @return int
	 */
	private function getLevelPopulationLimit()
	{
		$db = Common::getDB( $this->userId );
		$data = $db->find( 'lw_level' , array( 'lid' => $this->data['lid'] ) );
		return $data['houseNumLimit'] * Common::getConfig( 'every_house_can_live' );
	}
	
	/**
	 * 增加人口
	 * @param int $number 增加的数量 default 1
	 * @return boolean
	 */
	public function addPopulation( $number = 1 )
	{
		$tmpNum = $this->data['population'] + $number;
		if ( $tmpNum > $this->getPolulationLimit() )
		{
			return false;
		}
		$this->data['population'] = $tmpNum;
		$this->updateToDb( $this->_table , self::DATA_ACTION_UPDATE , array( 'population' => $this->data['population'] ) );
		$this->changeLevel();
		return true;
	}
	
	/**
	 * 增加房子
	 * @param int $number 要增加的数量
	 * @return boolean
	 */
	public function addHouse( $number )
	{
		$data = $this->getLevel();
		$tmpHouseNum = $this->data['houseNum'] + $number;
		if ( $tmpHouseNum > $data['houseNumLimit'] )
		{
			return false;
		}
		$this->data['houseNum'] = $tmpHouseNum;
		$this->updateToDb( $this->_table , self::DATA_ACTION_UPDATE , array( 'houseNum' => $this->data['houseNum'] ) );
		return true;
	}
	
	public static function addUser( $data = array() )
	{
		$data = array_merge( Common::getConfig( 'newUser' ) , $data );
		$db = Common::getDB();
		return $db->add( 'lw_user' , $data );
	}
	
	protected function formatToDBData( $table , $data )
	{
		return $data;
	}
	
	protected function formatFromDBData( $table , $data )
	{
		return $data;
	}
	
	protected function emptyDataWhenloadFromDB( $table )
	{
		return array();
	}
}
?>